package base

/*
* @author: hjz
* @time: 2025/4/26 19:48
*输入：nums = [1,2,3], k = 3 输出：2   [1,3,6]
 */
func subarraySum(nums []int, k int) int {
	sum := make([]int, len(nums))
	sum[0] = nums[0]
	for i := 1; i < len(nums); i++ {
		sum[i] = sum[i-1] + nums[i]
	}
	m := make(map[int]int, len(sum)>>1)
	m[0] = 1
	count := 0
	for i := range nums {
		var j int
		var ok bool
		if j, ok = m[sum[i]-k]; ok {
			count += j
		}
		m[sum[i]]++
	}
	return count
}
